package com.hmkj.web.controller.my;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.page.BasePageParams;
import com.hmkj.common.page.Pages;
import com.hmkj.core.mapper.account.AccountCashMapper;
import com.hmkj.core.mapper.account.AccountLogMapper;
import com.hmkj.core.mapper.account.AccountRechargeMapper;
import com.hmkj.core.po.account.AccountCash;
import com.hmkj.core.po.account.AccountLog;
import com.hmkj.core.po.account.AccountRecharge;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.account.AccountCashService;
import com.hmkj.core.service.account.AccountLogService;
import com.hmkj.core.service.account.AccountRechargeService;
import com.hmkj.core.service.account.AccountService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(description = "资金明细相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/fund")
public class AppAccountLogController extends GenericController<Integer, AccountLog> {
    @Resource
    private AccountCashService cashService;
    @Resource
    private AccountRechargeService rechargeService;
    @Resource
    private AccountLogService accountLogService;
    @Resource
    private AccountService accountService;

    @ApiOperation(value = "个人中心-资金明细-提现记录", notes = "提现记录")
    @ResponseBody
    @RequestMapping(value = "cash", method = RequestMethod.POST)
    public ResultEntity<Pages<AccountCash>> cashList(@ApiParam("类型") @RequestParam(required = false) Integer type,
                                                     @ModelAttribute BasePageParams basePageParams) {
        User user = getLoginUser();
        Map<String, Object> param = new HashMap<>();
        param.put("type", type);
        param.put("userId", user.getId());
        param.put("basePageParams", basePageParams);
        Pages<AccountCash> pages = cashService.selectByPage(basePageParams,
                q -> ((AccountCashMapper) q).findCountForCashList(param),
                q -> ((AccountCashMapper) q).findListForCashList(param));
        return ok(pages);
    }

    @ApiOperation(value = "个人中心-资金明细-充值记录", notes = "充值记录")
    @ResponseBody
    @RequestMapping(value = "recharge", method = RequestMethod.POST)
    public ResultEntity<Pages<AccountRecharge>> rechargeList(@ApiParam("类型") @RequestParam(required = false) Integer type,
                                                             @ModelAttribute BasePageParams basePageParams) {

        User user = getLoginUser();
        Map<String, Object> param = new HashMap<>();
        param.put("type", type);
        param.put("userId", user.getId());
        param.put("basePageParams", basePageParams);
        Pages<AccountRecharge> pages = rechargeService.selectByPage(basePageParams,
                q -> ((AccountRechargeMapper) q).findCountForRechargeList(param),
                q -> ((AccountRechargeMapper) q).findListForRechargeList(param));
        return ok(pages);
    }


    @ApiOperation(value = "个人中心-资金明细-资金记录", notes = "资金记录")
    @ResponseBody
    @RequestMapping(value = "log", method = RequestMethod.POST)
    public ResultEntity<Pages<AccountLog>> accountLogList(@ApiParam("类型 1钱包 2策略 0/null全部") @RequestParam(required = false) Integer type,
                                                          @ApiParam("开始时间") @RequestParam(required = false) String startTime,
                                                          @ApiParam("结束时间") @RequestParam(required = false) String endTime,
                                                          @ModelAttribute BasePageParams basePageParams) {
        User user = getLoginUser();
        Map<String, Object> param = new HashMap<>();
        param.put("type", type == null ? null : type == 0 ? null : type);
        if (startTime == null || startTime.equals("")) {
            startTime = "";
        }
        param.put("startTime", startTime);
        if (endTime == null || endTime.equals("")) {
            endTime = "";
        }
        param.put("endTime", endTime);
        param.put("userId", user.getId());
        param.put("basePageParams", basePageParams);
        Pages<AccountLog> pages = accountLogService.selectByPage(basePageParams,
                q -> ((AccountLogMapper) q).findCountForAccountLogList(param),
                q -> ((AccountLogMapper) q).findListForAccountLogList(param));
        return ok(pages);
    }
}
